package com.handy.plugin.data;

import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.handy.webwork.plugin.AbstractPlugin;
 
/**
 * data插件初始化。
 * @author rocken.zeng@gmail.com
 *
 */
public class Plugin extends AbstractPlugin {
	
	public void destroy() {
		log.info("销毁插件["+bean.getName()+"]");
	}

	public void run() {
		log.info("初始化插件["+bean.getName()+"]");
		//取配置文件handy_plugin_data
		loadProperties();
		//解析文件plugin_data_script_*.xml
		ScriptCache.loadScript();
		if (log.isDebugEnabled()){
			print();
		}
	}
	
	private void print(){
		log.info("===============================");
		log.info("dbkind = "+sc.getDbKind());
		log.info("iscache = "+sc.getIsCache());
		log.info("===============================");
		Set<Entry<String,ScriptBean>> scripts = sc.getScripts().entrySet();

		for (Map.Entry<String,ScriptBean> entry : scripts) {
			//String key = entry.getKey();
			ScriptBean bean = entry.getValue();
			log.info("id = "+bean.getId());
			log.info("filePath = "+bean.getFilePath());
			log.info("fileContent = "+bean.getFileContent());
		}
		log.info("===============================");
	}
	
	
	
	/**
	 * 读取配置文件handy_plugin_data.properties
	 */
	private void loadProperties() {
		try {
			PropertiesConfiguration config = new PropertiesConfiguration(
					"com/handy/plugin/data/files/handy_plugin_data.properties");
			sc.setIsCache(config.getInteger(ScriptCache.PROP_ISCACHE, new Integer(0)));
			sc.setDbKind(config.getInteger(ScriptCache.PROP_DBKIND, new Integer(1)));
			sc.setExtendScriptFile(config.getString(ScriptCache.PROP_EXTENDSCRIPTFILE, null));
			
		} catch (ConfigurationException e) {
			log.error(e.getMessage());
		}
	}

	private static Log log = LogFactory.getLog(Plugin.class);
	private ScriptCache sc = ScriptCache.getInstance();
}
